//
//  RealDetailsView.m
//  Streaming
//
//  Created by 官琛 on 15-6-27.
//  Copyright (c) 2015年 liuxun. All rights reserved.
//

#import "RealDetailsView.h"

@implementation RealDetailsView


-(instancetype)initWithFrame:(CGRect)frame newsId:(NSInteger)nsewID
{
    self = [super initWithFrame:frame];
    if (self) {
        NSString *str = [NSString stringWithFormat:@"%ld",nsewID];
        self.newsId = str;
        
        if (WIDTH == 320 && HEIGHT == 568) {
            [self reatView5];
        }
        if (WIDTH == 414 && HEIGHT == 736) {
            [self reatViewPlus];
        }
        if (WIDTH == 375 && HEIGHT == 667){
            [self reatView6];
        }

        
        [self afnGetRequest];
    }
    return self;
}

- (void)dealloc
{
    
    [_title1 release];
    [_nameLabel release];
    [_label release];
    
    [_webView release];
    [_webDetail release];
    [super dealloc];
}

-(void)reatView5
{
    
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(5, 70, WIDTH-10, HEIGHT-110)];
    self.webView.backgroundColor = [UIColor whiteColor];
    self.webView.delegate = self;
    [self.webView setScalesPageToFit:YES];
    [self addSubview:self.webView];
    [_webView autorelease];
    
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(250, -30, 30, 30);
    //    [self.button setBackgroundColor:[UIColor redColor]];
    [self.button setImage:[UIImage imageNamed:@"unCollect.png"] forState:UIControlStateNormal];
    
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    NSMutableArray * arrayId = [NSMutableArray array ];
    
    for (web *web1 in [[DateHandel shareHandle]selectAllCollect]) {
        
        [arrayId addObject:web1.idd ];
    }
    
    
    for (web *webm in [[DateHandel shareHandle]selectAllCollect]) {
        
        
        NSLog(@"array = %@ webm.idd =   %@  self.newsId =  %@",arrayId,webm.idd,self.newsId);
        
        if ([webm.idd isEqualToString:self.newsId]) {
            
            [self.button setImage:[UIImage imageNamed:@"collect.png"] forState:UIControlStateNormal];
            self.isCollect = YES;
            break;
        }
        else
        {
            [self.button setImage:[UIImage imageNamed:@"unCollect.png"] forState:UIControlStateNormal];
            
            self.isCollect = NO;
        }
    }
    
    [_webDetail autorelease];
    
    
    self.title1 = [[UILabel alloc]initWithFrame:CGRectMake(0, -60, WIDTH-10, 50)];
    //    self.title1.backgroundColor = [UIColor cyanColor];
    self.title1.numberOfLines = 0;
    self.title1.font = [UIFont boldSystemFontOfSize:18];
    [self.webView.scrollView addSubview:self.title1];
    [_title1 release];
    
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, -15, 100, 20)];
    self.nameLabel.font = [UIFont systemFontOfSize:10];
    [self.webView.scrollView addSubview:self.nameLabel];
    [_nameLabel release];
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(self.nameLabel.frame.origin.x+self.nameLabel.frame.size.width, -15, 80, 20)];
    self.label.font = [UIFont systemFontOfSize:10];
    [self.webView.scrollView addSubview:self.label];
    [_label release];
    
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.webView animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];
    [_HUD autorelease];
    
    
    
}



-(void)reatView6
{
    
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(5, 70, WIDTH-10, HEIGHT-110)];
    self.webView.backgroundColor = [UIColor whiteColor];
    self.webView.delegate = self;
    [self.webView setScalesPageToFit:YES];
    [self addSubview:self.webView];
    [_webView autorelease];
    
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(300, -30, 30, 30);
//    [self.button setBackgroundColor:[UIColor redColor]];
    [self.button setImage:[UIImage imageNamed:@"unCollect.png"] forState:UIControlStateNormal];
   
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    NSMutableArray * arrayId = [NSMutableArray array ];
    
    for (web *web1 in [[DateHandel shareHandle]selectAllCollect]) {
        
        [arrayId addObject:web1.idd ];
    }
    
    
    for (web *webm in [[DateHandel shareHandle]selectAllCollect]) {
        
        
        NSLog(@"array = %@ webm.idd =   %@  self.newsId =  %@",arrayId,webm.idd,self.newsId);
        
        if ([webm.idd isEqualToString:self.newsId]) {
            
            [self.button setImage:[UIImage imageNamed:@"collect.png"] forState:UIControlStateNormal];
            self.isCollect = YES;
            break;
        }
        else
        {
            [self.button setImage:[UIImage imageNamed:@"unCollect.png"] forState:UIControlStateNormal];
            
            self.isCollect = NO;
        }
    }
    
    [_webDetail autorelease];
    
    
    self.title1 = [[UILabel alloc]initWithFrame:CGRectMake(0, -60, WIDTH-10, 30)];
    //    self.title1.backgroundColor = [UIColor cyanColor];
    self.title1.font = [UIFont boldSystemFontOfSize:20];
    [self.webView.scrollView addSubview:self.title1];
    [_title1 release];
    
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, -20, 100, 20)];
    self.nameLabel.font = [UIFont systemFontOfSize:10];
    [self.webView.scrollView addSubview:self.nameLabel];
    [_nameLabel release];
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(self.nameLabel.frame.origin.x+self.nameLabel.frame.size.width, -20, 80, 20)];
    self.label.font = [UIFont systemFontOfSize:10];
    [self.webView.scrollView addSubview:self.label];
    [_label release];
    
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.webView animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];
    [_HUD autorelease];
    


}


-(void)reatViewPlus
{
    
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(5, 70, WIDTH-10, HEIGHT-110)];
    self.webView.backgroundColor = [UIColor whiteColor];
    self.webView.delegate = self;
    [self.webView setScalesPageToFit:YES];
    [self addSubview:self.webView];
    [_webView autorelease];
    
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(350, -30, 30, 30);
    //    [self.button setBackgroundColor:[UIColor redColor]];
    [self.button setImage:[UIImage imageNamed:@"unCollect.png"] forState:UIControlStateNormal];
    
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    NSMutableArray * arrayId = [NSMutableArray array ];
    
    for (web *web1 in [[DateHandel shareHandle]selectAllCollect]) {
        
        [arrayId addObject:web1.idd ];
    }
    
    
    for (web *webm in [[DateHandel shareHandle]selectAllCollect]) {
        
        
        NSLog(@"array = %@ webm.idd =   %@  self.newsId =  %@",arrayId,webm.idd,self.newsId);
        
        if ([webm.idd isEqualToString:self.newsId]) {
            
            [self.button setImage:[UIImage imageNamed:@"collect.png"] forState:UIControlStateNormal];
            self.isCollect = YES;
            break;
            
        }
        else
        {
            [self.button setImage:[UIImage imageNamed:@"unCollect.png"] forState:UIControlStateNormal];
            
            self.isCollect = NO;
            
        }
    }
    
    [_webDetail autorelease];
    
    
    self.title1 = [[UILabel alloc]initWithFrame:CGRectMake(0, -60, WIDTH-10, 30)];
    //    self.title1.backgroundColor = [UIColor cyanColor];
    self.title1.font = [UIFont boldSystemFontOfSize:20];
    [self.webView.scrollView addSubview:self.title1];
    [_title1 release];
    
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, -20, 100, 20)];
    self.nameLabel.font = [UIFont systemFontOfSize:10];
    [self.webView.scrollView addSubview:self.nameLabel];
    [_nameLabel release];
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(self.nameLabel.frame.origin.x+self.nameLabel.frame.size.width, -20, 80, 20)];
    self.label.font = [UIFont systemFontOfSize:10];
    [self.webView.scrollView addSubview:self.label];
    [_label release];
    
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.webView animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];
    [_HUD autorelease];
    
    
    
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '260%'"];
    
    //字体颜色
//    [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
//    //页面背景色
//    [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];


}


-(void)afnGetRequest
{
    
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = [NSString stringWithFormat:@"http://api.m.mtime.cn/News/Detail.api?newsId=%@",self.newsId];
    
   
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    
    
    
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//                NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        self.title1.text = [responseObject objectForKey:@"title"];
        //        self.webView = [responseObject objectForKey:@"content"];
        [self.webView loadHTMLString:[responseObject objectForKey:@"content"] baseURL:nil];
        self.nameLabel.text = [responseObject objectForKey:@"time"];
        self.label.text = [NSString stringWithFormat:@"来自%@", [responseObject objectForKey:@"source"]];
    
        self.webDetail = [[web alloc]init];
        self.webDetail.idd = self.newsId;
        self.webDetail.title1 = self.title1.text;

         [self.webView.scrollView addSubview:self.button];
         [self.HUD hide:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        [self.HUD hide:YES];
    }];
    
}
-(void)buttonAction:(UIButton *)button
{
//    NSLog(@"%ld",self.newsId);
    if (self.isCollect == NO) {
        [[DateHandel shareHandle]createCollectTable];
        [[DateHandel shareHandle]insertCollect:self.webDetail];
        self.isCollect = YES;
         [self.button setImage:[UIImage imageNamed:@"collect.png"] forState:UIControlStateNormal];
        UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [aler show];
        [aler release];
    }
    else
    {
        [[DateHandel shareHandle]deleteCollectTable:self.webDetail.idd];
        self.isCollect = NO;
        UIAlertView *aler1 = [[UIAlertView alloc]initWithTitle:@"提示" message:@"取消收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
         [self.button setImage:[UIImage imageNamed:@"unCollect.png"] forState:UIControlStateNormal];
        [aler1 show];
        [aler1 release];
    }
    
}

@end
